package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyPassKindBean;
import flc.ast.bean.MyPassRecordBean;
import flc.ast.databinding.ActivityPasswordDetailBinding;
import flc.ast.dialog.DeleteDialog;
import flc.ast.dialog.PasswordViewDialog;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.utils.SPUtil;

public class PasswordDetailActivity extends BaseAc<ActivityPasswordDetailBinding> {

    public static int position;
    private MyPassRecordBean recordBean;
    private boolean isPasswordVisible = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_password_detail;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPasswordDetailBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivDelete.setOnClickListener(this);
        mDataBinding.ivAccountCopy.setOnClickListener(this);
        mDataBinding.ivPasswordCopy.setOnClickListener(this);
        mDataBinding.ivPasswordShow.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivDelete://删除
                showDeleteDialog(position);
                break;
            case R.id.ivAccountCopy://用户名复制
                String account = mDataBinding.etAccount.getText().toString();
                if (TextUtils.isEmpty(account)) return;
                ClipboardUtils.copyText(account);
                ToastUtils.showShort(R.string.copy_suc);
                break;
            case R.id.ivPasswordCopy://密码复制
                String password = mDataBinding.etPassword.getText().toString();
                if (TextUtils.isEmpty(password)) return;
                ClipboardUtils.copyText(password);
                ToastUtils.showShort(R.string.copy_suc);
                break;
            case R.id.ivPasswordShow://密码显示隐藏
                togglePasswordVisibility();
                break;
        }
    }

    @Override
    protected void initData() {
        List<MyPassKindBean> listSp = AppSpUtils.getPasswordList();
        for (MyPassKindBean myPassKindBean : listSp) {
            if (myPassKindBean.getKind().equals(PasswordActivity.kindBean.getKind())) {
                recordBean = myPassKindBean.getList().get(position);
                mDataBinding.etName.setText(recordBean.getName());
                mDataBinding.etAccount.setText(recordBean.getAccount());
                mDataBinding.etPassword.setText(recordBean.getPassword());
                mDataBinding.etRemark.setText(recordBean.getRemarks());
            }
        }
    }

    //删除记录
    private void deleteRecord(int position) {
        List<MyPassKindBean> listSp = AppSpUtils.getPasswordList();
        for (MyPassKindBean myPassKindBean : listSp) {
            if (myPassKindBean.getKind().equals(PasswordActivity.kindBean.getKind())) {
                myPassKindBean.getList().remove(position);
            }
        }
        AppSpUtils.savePasswordList(listSp);
        //总条数+1
        int countPass = SPUtil.getInt(mContext, Content.countPass, 0);
        countPass--;
        SPUtil.putInt(mContext, Content.countPass, countPass);
        ToastUtils.showShort(getString(R.string.delete_suc));
        finish();
    }

    private void togglePasswordVisibility() {
        if (isPasswordVisible) {
            // 隐藏密码
            mDataBinding.etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            mDataBinding.ivPasswordShow.setImageResource(R.drawable.bkj); // 切换为不可见图标的资源
        } else {
            // 显示密码
            mDataBinding.etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            mDataBinding.ivPasswordShow.setImageResource(R.drawable.kj); // 切换为可见图标的资源
        }

        isPasswordVisible = !isPasswordVisible;
        // 强制光标更新，以确保密码的可见性立即更改
        mDataBinding.etPassword.setSelection(mDataBinding.etPassword.getText().length());
    }

    private void showDeleteDialog(int position) {
        DeleteDialog dialog = new DeleteDialog(mContext);
        dialog.setListener(new DeleteDialog.Ilistener() {

            @Override
            public void clickDelete() {
                deleteRecord(position);
            }

        });
        dialog.show();
    }

}